package com.linkgap.cloud.controller;

import com.linkgap.cloud.entity.Student;
import com.linkgap.cloud.model.Response;
import com.linkgap.cloud.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController {

    @GetMapping("/add/{num1}/{num2}")
    public Integer add(@PathVariable Integer num1, @PathVariable Integer num2) {
        return num1 + num2;
    }

    @Autowired
    private StudentService studentService;

    @GetMapping("/queryAll")
    public List<Student> query() {

        List<Student> query = studentService.query();
        return query;
    }


    @GetMapping("/queryById/{id}")
    public Student queryById(@PathVariable Long id) {

        //通过传一个id 这个queryById（id） 查询一个学生
        Student student = studentService.queryById(id);
        return student;
    }

    @GetMapping("/queryByName/{nameStr}")
    public List<Student> queryByNameLike(@PathVariable String nameStr) {
        return studentService.queryByNameLike(nameStr);

    }


    @GetMapping("/updateByName/{oldName}/{newName}")
    public boolean updateByName(@PathVariable String oldName, @PathVariable String newName) {
        return studentService.updateByName(oldName, newName);
    }


    @PostMapping("/insert")
    public boolean insert(@RequestBody Student student) {

        boolean result = studentService.insertStudent(student);
        return result;
    }


    @GetMapping("/queryById2")
    public Response<Student> queryById3(@RequestParam int age, @RequestParam String name) {

        return null;
    }

    @GetMapping("/queryById2/{id}")
    public Response<Student> queryById2(@PathVariable int id) {

        Student student = new Student();
        student.setName("张三");

        Response<Student> response = new Response<>();
        response.setData(student);

        try {
            int num = 10 / 0;
        } catch (Exception e) {
            response.setCode(101);
            response.setMsg("分母为0！");
        }

        return response;
    }


    @GetMapping("/queryListById2/{id}")
    public Response<List<Student>> queryListById2(@PathVariable int id) {

        List<Student> res = new ArrayList<>();
        Student student = new Student();
        student.setName("张三");
        res.add(student);

        Response<List<Student>> response = new Response<>();
        response.setData(res);

//        try {
//            int num = 10 / 0;
//        } catch (Exception e) {
//            response.setCode(101);
//            response.setMsg("分母为0！");
//        }

        return response;
    }
}
